//
//  HomeWaterFlowCell.m
//  YiKao2
//
//  Created by Mac Jc on 15/7/29.
//  Copyright (c) 2015年 Mac Jc. All rights reserved.
//

#import "HomeWaterFlowCell.h"
#import "AllHeader.h"

//const CGFloat kTMPhotoQuiltViewMargin = 0;

@implementation HomeWaterFlowCell

@synthesize photoView = _photoView;
@synthesize tagLabel = _tagLabel;


- (void)dealloc{
    [_photoView release],
    _photoView = nil;
    [_tagLabel release],
    _tagLabel = nil;
    
    [super dealloc];
}


//重写初始化方法
- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self.layer setCornerRadius:5];
        [UIView viewShadowColor:self];
        
        _textView = [[UIView alloc] init];
        _textView.frame = self.bounds;
        _textView.layer.cornerRadius = 5;
        //        _textView.layer.masksToBounds = YES;
        _textView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_textView];
        
        _photoView = [[UIImageView alloc] init];
        _photoView.contentMode = UIViewContentModeScaleAspectFill;
        _photoView.backgroundColor = kRandomColor;
        _photoView.frame =CGRectMake(0, 0, self.bounds.size.width, 50);
        //        [UIView viewRediusForLeftAndReightOfTop:_photoView withRedius:5];
        [_photoView setContentMode:(UIViewContentModeScaleAspectFill)];
        //        _photoView.clipsToBounds = YES;
        [self addSubview:_photoView];
        
        _describeLabel = [[UILabel alloc] init];
        _describeLabel.frame = CGRectMake(10, self.bounds.size.height - 100 * HEIGHT,(SCREEN_WIDTH - 30) / 2 - 20, 15);
        _describeLabel.textColor = kCustomColor(50, 50, 70, 1);
        _describeLabel.font = [UIFont systemFontOfSize:11];
        [self addSubview:_describeLabel];
        
        _tagLabel = [[UILabel alloc] init];
        _tagLabel.frame = CGRectMake(10, _describeLabel.bottom + 2 , self.bounds.size.width - 60, 11 * HEIGHT);
        _tagLabel.textColor = kCustomColor(150, 150, 150, 1);
        _tagLabel.font = [UIFont systemFontOfSize:10];
        [self addSubview:_tagLabel];
        
        _viewNumImageView = [[UIImageView alloc] init];
        _viewNumImageView.frame = CGRectMake(10, _tagLabel.bottom + 12 * HEIGHT, 15, 12);
        _viewNumImageView.image = [UIImage imageNamed:@"ViewNum"];
        [self addSubview:_viewNumImageView];
        
        _viewNumLabel = [[UILabel alloc] init];
        _viewNumLabel.frame = CGRectMake(_viewNumImageView.right + 2, _viewNumImageView.top - 3.5 * HEIGHT, 100, 20 * HEIGHT);
        _viewNumLabel.textColor = kCustomColor(180, 180, 180, 1);
        _viewNumLabel.font = [UIFont boldSystemFontOfSize:11];
        [self addSubview:_viewNumLabel];
        
        _CommentNumIcon = [[UIImageView alloc] init];
        _CommentNumIcon.frame = CGRectMake(60, _viewNumImageView.top, 15, 12);
        _CommentNumIcon.image = [UIImage imageNamed:@"CommentNumIcon"];
        [self addSubview:_CommentNumIcon];
        
        _CommentNumLabel = [[UILabel alloc] init];
        _CommentNumLabel.frame = CGRectMake(_CommentNumIcon.right + 2, _viewNumImageView.top - 2 * HEIGHT, 100, 15 * HEIGHT);
        _CommentNumLabel.textColor = kCustomColor(180, 180, 180, 1);
        _CommentNumLabel.font = [UIFont boldSystemFontOfSize:11];
        [self addSubview:_CommentNumLabel];
        
        _goodNumLabel = [[UILabel alloc] init];
        _goodNumLabel.textColor = kCustomColor(180, 180, 180, 1);
        _goodNumLabel.font = [UIFont boldSystemFontOfSize:11];
        [self addSubview:_goodNumLabel];
        
        _lineView = [[UIView alloc] init];
        _lineView.frame = CGRectMake(0, _viewNumImageView.bottom + 6 * HEIGHT, self.bounds.size.width, 0.5 * HEIGHT);
        _lineView.backgroundColor = [UIColor lightGrayColor];
        _lineView.alpha = 0.5;
        [self addSubview:_lineView];
        
        _iconView = [[UIImageView alloc] init];
        _iconView.frame = CGRectMake(10, _lineView.bottom + 6 * HEIGHT, 26, 26);
        [_iconView.layer setCornerRadius:13];
        [_iconView.layer setMasksToBounds:YES];
        [_iconView setContentMode:(UIViewContentModeScaleAspectFill)];
        [self addSubview:_iconView];
        
        _authorLabel = [[UILabel alloc] init];
        _authorLabel.frame = CGRectMake(_iconView.right +8, _iconView.top + 1 , self.bounds.size.width - 60, 15 * HEIGHT);
        _authorLabel.textColor = kCustomColor(40, 40, 50, 1);
        _authorLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:_authorLabel];
        
        _provinceLabel = [[UILabel alloc] init];
        _provinceLabel.frame = CGRectMake(_iconView.right + 8, _authorLabel.bottom + 1 , self.bounds.size.width - 60, 11 * HEIGHT);
        _provinceLabel.textColor = kCustomColor(150, 150, 150, 1);
        _provinceLabel.font = [UIFont systemFontOfSize:9];
        [self addSubview:_provinceLabel];
        
    }
    return self;
}


- (void)setModel:(HomeListModel *)model{
    
    _model = model;
    
    CGFloat width = [_model.imgweight floatValue];
    CGFloat height = [_model.imgheight floatValue];
    
    CGFloat imageHeight;
    
    if (width == 0 && height == 0) {
        imageHeight = 0;
    }else{
        imageHeight = height / width * ((SCREEN_WIDTH - 30) / 2);
    }
    
    _photoView.frame = CGRectMake(0, 0, (SCREEN_WIDTH - 30)/2, imageHeight);
    
    [_iconView sd_setImageWithURL:[NSURL URLWithString:model.userFace] placeholderImage:nil options:SDWebImageRetryFailed | SDWebImageLowPriority ];
    
    
    //        _textView.frame = CGRectMake(0, self.bounds.size.height - 104 * HEIGHT, self.bounds.size.width,52 * HEIGHT);
    
    _describeLabel.frame = CGRectMake(10, self.bounds.size.height - 100 * HEIGHT,(SCREEN_WIDTH - 30) / 2 - 20, 15);
    _tagLabel.frame = CGRectMake(10, _describeLabel.bottom + 2 , self.bounds.size.width - 60, 11 * HEIGHT);
    
    _viewNumImageView.frame = CGRectMake(10, _tagLabel.bottom + 10 * HEIGHT, 15, 12);
    _viewNumLabel.frame = CGRectMake(_viewNumImageView.right + 2, _viewNumImageView.top - 3.5 * HEIGHT, 100, 20 * HEIGHT);
    _CommentNumIcon.frame = CGRectMake(60, _viewNumImageView.top, 15, 12);
    _CommentNumLabel.frame = CGRectMake(_CommentNumIcon.right + 2, _viewNumImageView.top - 2 * HEIGHT, 100, 15 * HEIGHT);
    
    _lineView.frame = CGRectMake(0, _viewNumImageView.bottom + 8 * HEIGHT, self.bounds.size.width, 0.5 * HEIGHT);
    
    _iconView.frame = CGRectMake(10, _lineView.bottom + 6 * HEIGHT, 26, 26);
    _authorLabel.frame = CGRectMake(_iconView.right +8, _iconView.top + 1 , self.bounds.size.width - 60, 15 * HEIGHT);
    _provinceLabel.frame = CGRectMake(_iconView.right + 8, _authorLabel.bottom + 1 , self.bounds.size.width - 60, 11 * HEIGHT);
    
    [_photoView sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:nil options:SDWebImageRetryFailed | SDWebImageLowPriority];
    [UIView viewRediusForLeftAndReightOfTop:_photoView withRedius:5];
    
    _authorLabel.text = model.nickName;
    _provinceLabel.text = model.province;
    _tagLabel.text = model.tags;
    _describeLabel.text = model.info;
    _viewNumLabel.text = model.views;
    _CommentNumLabel.text = model.comments;
    _goodNumLabel.text = model.good;
}


+ (CGFloat)heightOfCell:(HomeListModel *)model{
    
    CGFloat width = [model.imgweight floatValue];
    CGFloat height = [model.imgheight floatValue];
    
    CGFloat imageHeight = height / width * ((SCREEN_WIDTH - 30) / 2);
    
    return 110 + imageHeight ;
}

@end
